package com.heihei.reduceimg;

import com.heihei.combine.util.DirFlagUtil;
import com.heihei.config.ConfigProperties;
import com.heihei.util.FileUtilF;
import com.heihei.util.ImgUtil;
import com.heihei.util.StrUtilF;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ReductImgStart {
    public static void main(String[] args) throws IOException {
        start();
    }

    /**
     * 开始执行  轻量化图片
     *
     * @param
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年04月07日 10:31:40
     */
    public static void start() throws IOException {
        System.out.println("请输入父级文件夹：");
        File parentDir = FileUtilF.getValidSrcDir();
        Integer flag = DirFlagUtil.getFlag();
        while (flag != 1 && flag != 2) {
            System.out.println("参数非法，请重新选择：");
            flag = DirFlagUtil.getFlag();
        }
        if(flag == 1) {
            reduceCurrentDirImg(parentDir);
        } else if(flag == 2) {
            for (File dir : parentDir.listFiles()) {
                reduceCurrentDirImg(dir);
            }
        }

    }

    /**
     * 处理当前目录
     *
     * @param parentDir
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年04月08日 17:01:03
     */
    private static void reduceCurrentDirImg(File parentDir) throws IOException {
        //获取目标DIr
        File targetDir = getValicTargetDir(parentDir);
        FileUtilF.handleFiles(parentDir, file -> {
            if(file.isFile()) {
                try {
                    System.out.println("开始处理：" + new File(targetDir, file.getName()).getAbsolutePath());
                    writeImg(file, parentDir, targetDir);
                    System.out.println("轻量化成功：" + new File(targetDir, file.getName()).getAbsolutePath());
                } catch (IOException e) {
                    System.out.println("写文件出错： " + file.getAbsolutePath());
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 写图片文件
     *
     * @param file
     * @param parentDir
     * @param targetDir
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年04月07日 10:40:39
     */
    private static void writeImg(File file, File parentDir, File targetDir) throws IOException {
        String imgStyle = null;
        String fileName = file.getName().toLowerCase();

        String childPath = file.getAbsolutePath().substring(parentDir.getAbsolutePath().length());
        File targetFile = new File(targetDir, childPath);

        if(fileName.endsWith(".jpg")) {
            imgStyle = "JPG";
        } else if(fileName.endsWith(".png")) {
            imgStyle = "PNG";
        } else {
            FileUtilF.copyFile(file, targetFile);
            return;
        }

        BufferedImage bufferedImage = ImgUtil.loadImageLocal(file);
        int width = 0;
        int height = 0;
        if(bufferedImage.getWidth() > bufferedImage.getHeight()) {
            width = ConfigProperties.REDUCE_ACCROSS_IMG_WIDTH;
        } else {
            width = ConfigProperties.REDUCE_VERTICAL_IMG_WIDTH;
        }
        height = (int)(1.0 * width * bufferedImage.getHeight() / bufferedImage.getWidth());
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = tag.createGraphics();
        graphics.drawImage(bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, width, height, null);
        graphics.dispose();
        if(!targetFile.getParentFile().exists()) {
            targetFile.getParentFile().mkdirs();
        }
        ImageIO.write(tag, imgStyle, targetFile);
    }

    /**
     * 获取有效的targetDir
     *
     * @param parentDir
     * @Return {@link File}
     * @throws
     * @author duanhengfei
     * @date 2021年04月07日 10:33:27
     */
    private static File getValicTargetDir(File parentDir) {
        int i = 1;
        File targetDir = new File(parentDir.getParentFile(), parentDir.getName() + StrUtilF.getNumberString(i++, 4));
        while (targetDir.exists()) {
            targetDir = new File(parentDir.getParentFile(), parentDir.getName() + StrUtilF.getNumberString(i++, 4));
        }
        targetDir.mkdirs();
        return targetDir;
    }
}
